/* Client.java
 *
 * Author:	Malcolm Staudacher
 * Date:	Oct 31, 2008
 *
 */

package fhv.itb.ec.atm;

import org.omg.CORBA.ORB;
import org.omg.CORBA.ORBPackage.InvalidName;
import org.omg.CosNaming.NamingContextExt;
import org.omg.CosNaming.NamingContextExtHelper;
import org.omg.CosNaming.NamingContextHelper;
import org.omg.CosNaming.NamingContextPackage.CannotProceed;
import org.omg.CosNaming.NamingContextPackage.NotFound;


public class ATMClient {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		
		ATMFactory atmf = null;
		ATM atm = null;
		
		// create ORB
		ORB orb = ORB.init(args, null);
		
		try {
			// get naming context
			org.omg.CORBA.Object objRef = orb.resolve_initial_references("NameService");
			NamingContextExt ncRef = NamingContextExtHelper.narrow(objRef);
			
			atmf = ATMFactoryHelper.narrow(ncRef.resolve_str("ATMFactory"));
			System.out.println("Obtained reference to ATMFactory");
			
			if (atmf != null) {
				atm = atmf.createATM();
			}
			
			if (atm != null) {
				System.out.println("Obtained reference to ATM\n");
			}
			
		} catch (InvalidName e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (NotFound e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (CannotProceed e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (org.omg.CosNaming.NamingContextPackage.InvalidName e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		// get initial account balance
		System.out.println("Initial Balances");
		System.out.println("Balance(1): "+atm.getBalance(1));
		System.out.println("Balance(2): "+atm.getBalance(2));
		System.out.println("Balance(3): "+atm.getBalance(3));
		System.out.println();
		
		// make $1000 depoist in account 1 and get new balance
		System.out.println("Depositting(1): 1000 ");
		atm.deposit(1, 1000);
		System.out.println("Balance(1): "+atm.getBalance(1));
		
		// make $100 withdrawal from account 2 and get new balance
		System.out.println("Withdrawing(2): 100 ");
		atm.withdraw(2, 100);
		System.out.println("Balance(2): "+atm.getBalance(2));
		
		// make $500 deposit in account 3 and get new balance
		System.out.println("Depositting(3): 500 ");
		atm.deposit(3, 500);
		System.out.println("Balance(3): "+atm.getBalance(3));
		
		// get final account balance
		System.out.println();
		System.out.println("Final Balances");
		System.out.println("Balance(1): "+atm.getBalance(1));
		System.out.println("Balance(2): "+atm.getBalance(2));
		System.out.println("Balance(3): "+atm.getBalance(3));
	}

}
